<?php

class Application_Model_Recommend extends Application_Model_DomainObject {
    
    protected $_id;
    protected $_userId; //注册用户id
    protected $_reUserId;//推荐人id
    protected $_createTime;
    protected $_isAbled;//是否有效
    /**
     * 有效的推荐
     */
    const IsAbled_Complete = 1;
    /**
     * 无效的推荐
     */
    const IsAbled_Unfinished = 2;

    
    
    public function getUserItem($userId) {
        return Application_Model_User::load($userId);
    }
    
    private $_countRebatesWay;
    public function getCountRebatesWay($isAbled) {
        $this->_countRebatesWay = Application_Model_Reward::CountByUserIdIsAbled($this->_userId, $isAbled);
        return $this->_countRebatesWay;
    }
    
    private $_sumRebatesWay;
    public function getSumRebatesWay($isAbled) {
        $this->_sumRebatesWay = Application_Model_Reward::SumByUserIdIsAbled($this->_userId, $isAbled);
        return $this->_sumRebatesWay;
    }

    public static function getIsAbledAll()
    {
        return array(
            self::IsAbled_Complete => "有效"
            , self::IsAbled_Unfinished => "无效"
        );
    }

    public function getIsAbledName() {
        $Arr_isAbled = self::getIsAbledAll();
        return $Arr_isAbled[$this->_isAbled];
    }

    public static function getFiltel($quick,$startTime,$entTime,$userId,$pagesize,$pageindex)
    {
        $where = array();
        $and = "";
        if($quick != "") {
            $userItem = Application_Model_User::getByNikeNameStore($quick);
            if($userItem) {
                $userArrId = "(";
                foreach ($userItem as $userVal) {
                    $userArrId .= $userVal->Id.",";
                }
                $userArrId .= "0)";
                $and = "(";
                $where[$and." userId in "] = $userArrId;
                $and = " or ";
                $where[$and." reUserId in "] = $userArrId;
                $and = " ) and ";
                $where[$and." 1 = "] = 1;
                $and = " and ";
            }
        }
        
        if($startTime != "") {
            $where[$and." createTime >= "] = $startTime;
            $and = " and ";
        }
        
        if($entTime != "") {
            $where[$and." createTime <= "] = $entTime." 23:59:59";
            $and = " and ";
        }
        
        if($userId != "") {
            $where[$and." reUserId = "] = $userId;
            $and = " and ";
        }
        return self::getDao()->queryPage($where, 'id desc', $pagesize, $pageindex);;
    }
    
    public static function addRecommend($userId,$reUserId)
    {
        $RecommendItem = new Application_Model_Recommend();
        $RecommendItem->UserId = $userId;
        $RecommendItem->ReUserId = $reUserId;
        $RecommendItem->CreateTime = date("Y-m-d H:i:s");
        $RecommendItem->IsAbled = self::IsAbled_Unfinished;
        $RecommendItem->save();
    }
    
    public static function ByUserIdReUserId($userId,$reUserId)
    {
        $where = array();
        $and = "";
        if($userId != null){
            $where[" userId = "] = $userId;
            $and = " and ";
        }
        if($reUserId != null) {
            $where[$and." reUserId = "] = $reUserId;
        }
        return self::getDao()->queryOne($where);
    }
    

    public function getSumScore()
    {
        $rewardItem = Application_Model_Reward::CountByUserId($this->_userId);
        return 50+$rewardItem*100;
    }
}
